package admin

import (
	"context"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/module/admin/resourceLogic"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/utils/database"
	"grpc.getaway.adminspro.padmins.com/pkg/config/businessError"
	"grpc.getaway.adminspro.padmins.com/pkg/libs/utils"
	pbAdmin "grpc.getaway.adminspro.padmins.com/pkg/proto/admin"
	"strconv"
)

func (rpc *admin) AdminResourceEdit(ctx context.Context, pb *pbAdmin.AdminResourceEditReq) (*pbAdmin.AdminResourceEditResp, error) {
	list := resourceLogic.List()
	if pb.AdminResource == nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.ErrorfByErrorInfo(businessError.ParamsInvalidF, "AdminResource不能为空"))
	}
	if _, ok := list["status"][strconv.FormatInt(int64(pb.AdminResource.Status), 10)]; !ok {
		return nil, businessError.ErrorInfoToRpcError(businessError.ErrorfByErrorInfo(businessError.ParamsInvalidF, "Status取值范围错误"))
	}
	if _, ok := list["type"][strconv.FormatInt(int64(pb.AdminResource.Status), 10)]; !ok {
		return nil, businessError.ErrorInfoToRpcError(businessError.ErrorfByErrorInfo(businessError.ParamsInvalidF, "Type取值范围错误"))
	}
	if pb.AdminResource.Path == "" {
		return nil, businessError.ErrorInfoToRpcError(businessError.ErrorfByErrorInfo(businessError.ParamsInvalidF, "Path不能为空"))
	}
	if pb.AdminResource.Resource == "" {
		return nil, businessError.ErrorInfoToRpcError(businessError.ErrorfByErrorInfo(businessError.ParamsInvalidF, "Resource不能为空"))
	}
	if pb.AdminResource.Title == "" {
		return nil, businessError.ErrorInfoToRpcError(businessError.ErrorfByErrorInfo(businessError.ParamsInvalidF, "Title不能为空"))
	}
	if pb.AdminResource.Weigh < 0 {
		return nil, businessError.ErrorInfoToRpcError(businessError.ErrorfByErrorInfo(businessError.ParamsInvalidF, "weigh不能小于0"))
	}

	Db := database.Db.GetDefaultDb(ctx)
	if pb.AdminResource.Pid > 0 {
		hasPid, dbErr := Db.Find("select * from admin_resource where type=1 and id=?", pb.AdminResource.Pid)
		if dbErr != nil {
			return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
		}
		if hasPid == nil {
			return nil, businessError.ErrorInfoToRpcError(businessError.Error(businessError.OperateError.Code, "pid菜单不存在"))
		}
		rows, dbErr := Db.Query("select * from admin_resource where type=1")
		if dbErr != nil {
			return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
		}
		ChildrenIds := utils.NewTree().Init(rows).GetChildrenIds(pb.AdminResource.Id, true)
		if utils.InArray(pb.AdminResource.Pid, ChildrenIds) {
			return nil, businessError.ErrorInfoToRpcError(businessError.Error(businessError.OperateError.Code, "pid菜单不能为自己id或者是下级id在"))
		}
	}

	hasPath, dbErr := Db.Find("select * from admin_resource where path=? and id<>?", pb.AdminResource.Path, pb.AdminResource.Id)
	if dbErr != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
	}

	if hasPath != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.Error(businessError.OperateError.Code, "路径已存在"))
	}

	hasResource, dbErr := Db.Find("select * from admin_resource where resource=? and id<>?", pb.AdminResource.Resource, pb.AdminResource.Id)
	if dbErr != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
	}
	if hasResource != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.Error(businessError.OperateError.Code, "资源已存在"))
	}

	row, dbErr := Db.Find("select * from admin_resource where id=?", pb.AdminResource.Id)
	if dbErr != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
	}
	if row == nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.NotFound)
	}

	rowCount, dbErr := Db.Update("admin_resource", map[string]interface{}{
		"pid":         pb.AdminResource.Pid,
		"type":        pb.AdminResource.Type,
		"path":        pb.AdminResource.Path,
		"resource":    pb.AdminResource.Resource,
		"title":       pb.AdminResource.Title,
		"icon":        pb.AdminResource.Icon,
		"remark":      pb.AdminResource.Remark,
		"badge":       pb.AdminResource.Badge,
		"weigh":       pb.AdminResource.Weigh,
		"status":      pb.AdminResource.Status,
		"create_time": utils.Date("Y-m-d H:i:s"),
	}, map[string]interface{}{
		"id": pb.AdminResource.Id,
	})

	if dbErr != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
	}

	if rowCount <= 0 {
		return nil, businessError.ErrorInfoToRpcError(businessError.OperateError)
	}

	return &pbAdmin.AdminResourceEditResp{
		RowCount: rowCount,
	}, nil
}
